package com.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.entity.People;
import com.example.entity.restTemplate.MyRestTemplate;
import com.example.service.PeopleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/people")
@Api(description = "人员测试接口")
public class PeopleController {

    @Autowired
    private PeopleService peopleService;

    @ApiOperation(value = "查询", notes = "查询所有人员")
    @GetMapping("/selectAll")
    public List<People> findAllPeople() {
        List<People> list = peopleService.findAll();
        return list;
    }

    @ApiOperation(value = "查询", notes = "通过id查询(Get)")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "人员id", required = true, dataType = "int")})
    @GetMapping("/selectById")
    public People findByIdGet(@RequestParam Integer id) {
        People people = peopleService.findById(id);
        return people;
    }

    @ApiOperation(value = "查询", notes = "通过id查询(Post)")
    @PostMapping("/selectById")
    public People findByIdPost(@RequestParam Integer id) {
        People people = peopleService.findById(id);
        return people;
    }


    @ApiOperation(value = "查询", notes = "通过People查询")
    @PostMapping("/selectByPeople")
    public JSONObject findByPeople(@RequestBody People people) {
        People people2 = peopleService.findById(people.getId());
        JSONObject result = new JSONObject();
        result.put("data", people2);
        return result;
    }


    @ApiOperation(value = "查询", notes = "通过id查询,restful风格")
    @GetMapping("/selectById/{id}")
    public String findById2(@PathVariable("id") Integer id) {
        People people = peopleService.findById(id);
        return people == null ? "{message:查询失败，人员不存在}" : "{查询成功:" + people + "}";
    }

    @ApiOperation(value = "添加", notes = "添加用户")
    @PostMapping("/addPeople")
    public String addPeople(@RequestBody People people) {
        int row = peopleService.add(people);
        return row == 1 ? "添加成功" : "添加失败";
    }

    @ApiOperation(value = "添加", notes = "添加数据")
    @PostMapping("/postData")
    public String postData(@RequestParam Integer id, @RequestBody JSONObject object) {
        System.out.println(object);
        return object.toJSONString();
    }

    @ApiOperation("测试restTemplate")
    @PostMapping("/test")
    public JSONObject test1() {
        String url = null;
        new MyRestTemplate().template(url, MediaType.APPLICATION_JSON_VALUE, null, null);
        return null;
    }


}
